package com.thoughtworks.mail;

import java.util.List;
import java.util.ArrayList;

public class MailReader {
    private static final MailReader mailReader = new MailReader();

    private MailReader() {
    }

    public static MailReader getInstance() {
        return MailReader.mailReader;  
    }

    public List<Mail> getMails(String serverName, String dbName, String password) throws NotesInvokeException{
        ArrayList<Mail> mails = new ArrayList<Mail>();
        long pointer = DominoDAO.nativeGetHeadPointer(serverName, dbName, password);
        if(pointer==-1){
             throw new NotesInvokeException();
        }
        MailWrapper mailWrapper = new MailWrapper(pointer, true);

        long nextPointer = mailWrapper.getNextPointer();

        while (nextPointer != 0L) {
            mailWrapper = new MailWrapper(nextPointer, false);
            nextPointer = mailWrapper.getNextPointer();
            mails.add(mailWrapper.getMail());
        }
        
        return mails;
    }
}
